/*
 * jlb
 */
package com.jlb.user.service;

import com.jlb.core.entity.Page;
import com.jlb.core.exceptions.BaseException;
import com.jlb.core.exceptions.UserSubAccountException;
import com.jlb.user.dao.UserSubAccountDAO;
import com.jlb.user.entity.UserSubAccountState;
import com.jlb.user.entity.UserSubAccount;
import com.jlb.core.base.BaseDAO;
import com.jlb.core.base.BaseSVImpl;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.HashMap;
import java.util.Date;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import com.baidu.fsg.uid.UidGenerator;

@Service("userSubAccountSV")
@Slf4j
public class UserSubAccountSVImpl extends BaseSVImpl<UserSubAccount, Long> implements UserSubAccountSV {

    @Autowired
    private UserSubAccountDAO userSubAccountDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return userSubAccountDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(UserSubAccount entity) throws BaseException {
        entity.setCreateTime(new Date());
        super.save(entity);
    }

    /**
     * 加载一个对象UserSubAccount
     *
     * @param id
     * @return UserSubAccount
     */
    @Override
    public UserSubAccount load(java.lang.Long id) {
        if (id == null) {
            throw new UserSubAccountException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        return userSubAccountDAO.load(param);
    }

    /**
     * 加载一个对象UserSubAccount 通过id
     *
     * @param id
     * @return UserSubAccount
     */
    @Override
    public UserSubAccount loadById(java.lang.Long id) {
        if (id == null) {
            throw new UserSubAccountException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return userSubAccountDAO.loadById(id);
    }


    /**
     * 根据主键id,oldStates 共同更新 UserSubAccount 的状态到newState状态
     *
     * @param id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(java.lang.Long id, UserSubAccountState newState, UserSubAccountState... oldStates) {
        if (id == null) {
            throw new UserSubAccountException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new UserSubAccountException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new UserSubAccountException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        userSubAccountDAO.updateStateById(id, new Date(), newState, oldStates);
    }

    /**
     * 根据主键id 更新 UserSubAccount 的状态到另一个状态
     *
     * @param id
     * @param state 状态
     */
    @Override
    public void updateById(java.lang.Long id, UserSubAccountState state) {
        if (id == null) {
            throw new UserSubAccountException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        userSubAccountDAO.updateById(id, state, new Date());
    }


    /**
     * 删除对象UserSubAccount
     *
     * @param id
     * @return UserSubAccount
     */
    @Override
    public void delete(java.lang.Long id) {
        if (id == null) {
            throw new UserSubAccountException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        userSubAccountDAO.delete(param);
    }


    /**
     * 查询UserSubAccount分页
     *
     * @param userSubAccount 对象
     * @param offset         查询开始行
     * @param limit          查询行数
     * @return List<UserSubAccount>
     */
    @Override
    public List<UserSubAccount> list(UserSubAccount userSubAccount, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (userSubAccount != null) {
            map = JSON.parseObject(JSON.toJSONString(userSubAccount, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return userSubAccountDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(UserSubAccount userSubAccount) {
        Map<String, Object> map = null;
        if (userSubAccount != null) {
            map = JSON.parseObject(JSON.toJSONString(userSubAccount, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return userSubAccountDAO.count(map);
    }

    /**
     * 查询UserSubAccount分页
     *
     * @param id
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<UserSubAccount>
     */
    @Override
    public List<UserSubAccount> list(java.lang.Long id, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        return userSubAccountDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(java.lang.Long id) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        return userSubAccountDAO.count(param);
    }

}
